/**
 * @author 黄泽毅
 */

//分页
//myData为待会要处理的数据，evit为'up'或'down'或页码，oldn为之前的页码,id为状态码
function decPageIndex(myData, evit, oldn, id) {
    questionnaires = getDataFromLocal("questionnaires");
    const D = new Date().toLocaleDateString();
    const now = timeFormat(D.split('/')[0]) + timeFormat(D.split('/')[1]) + timeFormat(D.split('/')[2]);
    questionnaires.forEach(item => {
        const date = timeFormat(item.deadline.split('-')[0]) + timeFormat(item.deadline.split('-')[1]) + timeFormat(item.deadline.split('-')[2]);
        if (Number(now) > Number(date) && item.status === 1) {
            item.status = 2;
        }
    })
    saveDataToLocal(questionnaires, 'questionnaires');
    let state = id;
    let totalCount; //文件总数据
    let tempData = [];
    myData.forEach(ems => {
        if (id == -1) {
            tempData.push(ems);
        } else if (ems.status == id) {
            tempData.push(ems);
        }
    });

    totalCount = tempData.length;
    let currentPage = 1; //用户传入当前页
    let pageSize = 5; //每一页默认5条数据
    //计算出来的
    let totalPage; //总页数

    totalPage = totalCount % pageSize == 0 ? parseInt(totalCount / pageSize) : parseInt(totalCount / pageSize) + 1;
    oldn = Number(oldn);
    if (evit === "up") {
        oldn = oldn - 1 >= 1 ? oldn - 1 : 1;
    } else if (evit === "down") {
        oldn = oldn + 1 <= totalPage ? oldn + 1 : totalPage;
    } else {
        oldn = evit
    }

    currentPage = oldn;
    //图表页清除所有a标签
    Array.from($(".num")).forEach(item => {
        item.remove();
    });

    //用来接收对象
    let start_end = pageIndex(3, Number(currentPage), totalPage);
    //改变原来数据的list数组
    //展示数据的开始索引
    let sta_Data = (currentPage - 1) * pageSize;
    tempData.splice(0, sta_Data);
    if (tempData.length > pageSize) {
        tempData.splice(pageSize, myData.length - pageSize);
    }
    // 重新渲染页面的索引业数
    let start = Number(start_end.beginIndex);
    let end = Number(start_end.endIndex);
    for (let i = start; i <= end; i++) {
        let el = document.createElement("a");
        el.innerText = i;
        el.className = "num";
        el.href = "javascript:void(0)";
        if (i == Number(currentPage)) {
            el.classList.add("on");
        }
        let down = $(".down")[0];

        $(".pagination")[0].insertBefore(el, down);
    }
    saveDataToLocal(tempData, "tempData");
    initial(Number(state));
    if (tempData.length == 0) {
        $(".up")[0].style.display = "none";
        $(".down")[0].style.display = "none";
    } else {
        $(".up")[0].style.display = "inline";
        $(".down")[0].style.display = "inline";
    }
}

decPageIndex(getDataFromLocal("questionnaires"), 1, 1, -1);

//监听点击事件
$(".pagination")[0].addEventListener("click", (e) => {
    let myData = getDataFromLocal("questionnaires");
    let oldn = $(".on")[0].innerText; //原来的页码
    let tempId = localStorage.getItem("tempId");
    let event = e.target;
    if (event.className.indexOf("iconfont") != -1) {
        event = event.parentNode;
    }
    if (event.href != null) {
        $(".on")[0].classList.remove("on");
        if (event.getAttribute("mask") === "up") {
            decPageIndex(myData, "up", oldn, tempId)
        } else if (event.getAttribute("mask") === "down") {
            decPageIndex(myData, "down", oldn, tempId)
        } else {
            decPageIndex(myData, event.innerText, oldn, tempId)
        }
    }
});
